<!--
 * @Author: LiYuan
 * @Date: 2021-02-02 15:10:38
 * @LastEditTime: 2021-02-02 17:34:44
 * @LastEditors: LiYuan
 * @Description: 办理出入库-批量办理弹窗
 * @You build it, You run it.
-->
<template>
  <el-dialog :title="title" :visible.sync="visible" :width="width" @close="handleClose">
    <mul-handle-form ref="mulHandleForm" v-if="visible" :formdata="formdata" :mulHandleKey="mulHandleKey"></mul-handle-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="ghost" @click="visible = false">{{ $t('common.base.cancel') }}</el-button>
      <el-button type="primary" @click="mulHandleSave" :disabled="pending" :loading="pending">{{ $t('common.base.makeSure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import MulHandleForm from '../form/MulHandleForm';
import { approveBatchByApproveNos } from '@src/api/SparePart';
import { t } from '@src/locales'

export default {
  name: 'batch-handle-dialog',
  components: {
    MulHandleForm
  },
  props: {
    title: {
      type: String,
      default: t('part.title1')
    },
    width: {
      type: String,
      default: "90%"
    },
    formdata: {
      type: Array,
      default: () => []
    },
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data: () => {
    return {
      visible: false,
      pending: false,
      mulHandleKey: 1
    }
  },
  watch: {
    dialogVisible (val) {
      this.visible = val
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },

    // 批量办理-保存
    mulHandleSave() {
      let form = this.$refs.mulHandleForm;
      if (form.pending) return
      if (form.selected.length === 0) {
        this.$platform.alert(t('part.tips1'));
        return
      }
      if (form.remark.length > 500) {
        this.$platform.alert(t('part.tips2', {num:500}));
        return
      }
      const existZero = form.selected.find(item => item.handleNum == 0);
      if (existZero) {
        this.$platform.alert(t('part.tips3'));
        return
      }
      const existMore = form.selected.find(item => {
        const decimals = Math.max(this.countDecimals(item.variation), this.countDecimals(item.solvedVariation));
        const max = (item.variation - item.solvedVariation).toFixed(decimals);
        return item.handleNum > max
      });
      if (existMore) {
        this.$message({
          type: 'warning',
          message: t('part.tips4')
        });
        return
      }

      if (this.pending) return;

      this.pending = true;
      const params = form.selected.map(item => {
        return {
          approveNo: item.approveNo,
          id: item.id,
          remark: form.remark,
          solvedVariation: item.handleNum,
          type: item.type
        }
      });
      approveBatchByApproveNos(params).then(res => {
        this.pending = false;
        if (res.success) {
          this.$message({
            showClose: true,
            message: res.message || t('common.base.dealWidthSuccess'),
            type: 'success'
          });
          setTimeout(() => {
            this.$emit('success')
          }, 1000)
        } else {
          this.$message({
            showClose: true,
            message: res.message,
            type: 'error'
          });
        }
      }).catch(err => {
        this.pending = false;
        this.$message({
          showClose: true,
          message: err.message,
          type: 'error'
        });
      })
    },

    // 获取小数位数
    countDecimals(num) {
      if (Math.floor(num) === num) return 0;
      return num.toString().split('.')[1].length || 0;
    },
  }
}
</script>

<style>
</style>